package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EBusinessInfo;
import com.ruoyi.system.domain.EBusinessShareholder;
import com.ruoyi.system.entity.BusinessShareholder;
import com.ruoyi.system.entity.SocialInfo;
import com.ruoyi.system.service.QiXinBaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EBusinessSocialMapper;
import com.ruoyi.system.domain.EBusinessSocial;
import com.ruoyi.system.service.IEBusinessSocialService;

/**
 * 社保信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@Service
public class EBusinessSocialServiceImpl implements IEBusinessSocialService
{
    @Autowired
    private EBusinessSocialMapper eBusinessSocialMapper;
    @Autowired
    QiXinBaoService qiXinBaoService;
    /**
     * 查询社保信息
     *
     * @param id 社保信息主键
     * @return 社保信息
     */
    @Override
    public EBusinessSocial selectEBusinessSocialById(Long id)
    {
        return eBusinessSocialMapper.selectEBusinessSocialById(id);
    }

    /**
     * 查询社保信息列表
     *
     * @param eBusinessSocial 社保信息
     * @return 社保信息
     */
    @Override
    public List<EBusinessSocial> selectEBusinessSocialList(EBusinessSocial eBusinessSocial)
    {
        return eBusinessSocialMapper.selectEBusinessSocialList(eBusinessSocial);
    }

    /**
     * 新增社保信息
     *
     * @param eBusinessSocial 社保信息
     * @return 结果
     */
    @Override
    public int insertEBusinessSocial(EBusinessSocial eBusinessSocial)
    {
        return eBusinessSocialMapper.insertEBusinessSocial(eBusinessSocial);
    }

    /**
     * 修改社保信息
     *
     * @param eBusinessSocial 社保信息
     * @return 结果
     */
    @Override
    public int updateEBusinessSocial(EBusinessSocial eBusinessSocial)
    {
        return eBusinessSocialMapper.updateEBusinessSocial(eBusinessSocial);
    }

    /**
     * 批量删除社保信息
     *
     * @param ids 需要删除的社保信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessSocialByIds(Long[] ids)
    {
        return eBusinessSocialMapper.deleteEBusinessSocialByIds(ids);
    }

    /**
     * 删除社保信息信息
     *
     * @param id 社保信息主键
     * @return 结果
     */
    @Override
    public int deleteEBusinessSocialById(Long id)
    {
        return eBusinessSocialMapper.deleteEBusinessSocialById(id);
    }

    @Override
    public SocialInfo getSocialInfo(String discountApplicantCardCode,LoginUser loginUser) {
        EBusinessSocial eBusinessSocial=getSocialInfoByCodeScope(discountApplicantCardCode,loginUser);
        SocialInfo socialInfo=new SocialInfo();
        if(null==eBusinessSocial){
            eBusinessSocial=new EBusinessSocial();
            socialInfo=qiXinBaoService.getSocialInfoByCode(discountApplicantCardCode,loginUser);
            if(StringUtils.isNotEmpty(socialInfo.getStatus())&&"200".equals(socialInfo.getStatus())){
                //请求启信包接口
                eBusinessSocial.setBusinessCode(discountApplicantCardCode);
                eBusinessSocial.seteBusinessSocialMsg(JSON.toJSONString(socialInfo));
                eBusinessSocial.setUserId(loginUser.getUserId());
                eBusinessSocial.setDeptId(loginUser.getDeptId());
                insertEBusinessSocial(eBusinessSocial);
            }

        }else{
            socialInfo=JSON.parseObject(eBusinessSocial.geteBusinessSocialMsg(),SocialInfo.class);
        }
        return socialInfo;
    }

    public EBusinessSocial getSocialInfoByCodeScope(String discountApplicantCardCode, LoginUser loginUser){
        EBusinessSocial eBusinessSocial=new EBusinessSocial();
        eBusinessSocial.setBusinessCode(discountApplicantCardCode);
        eBusinessSocial.setDeptId(loginUser.getDeptId());
        return  eBusinessSocialMapper.getSocialInfoByCode(eBusinessSocial);
    }
}
